package com.changgou.goods.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/api/brand")
@RestController
@Api(value = "品牌管理接口", description = "品牌管理接口，提供品牌的增删改查")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @ApiOperation("查询品牌列表")
    @GetMapping("/findAll")
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", brandService.findList());
    }

    @ApiOperation("根据ID查询品牌")
    @GetMapping("/findOne/{id}")
    public Result findOne(@PathVariable("id") Integer id) {
        return new Result(true, StatusCode.OK, "查询成功", brandService.findById(id));
    }

    @ApiOperation("新增品牌")
    @PostMapping("/add")
    public Result add(Brand brand) {
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "保存成功");
    }

    @ApiOperation("修改品牌")
    @PostMapping("/update")
    public Result update(Brand brand) {
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "保存成功");
    }

    @ApiOperation("根据Id删除品牌")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @ApiOperation("多条件搜索品牌列表")
    @PostMapping("/search")
    public Result findByCondition(@RequestBody Map<String, Object> searchMap) {
        return new Result(true, StatusCode.OK, "查询成功", brandService.findList(searchMap));
    }

    @ApiOperation("分页查询品牌列表")
    @GetMapping("/searchByPage/{page}/{size}")
    public Result searchByPage(@PathVariable Integer page, @PathVariable Integer size) {
        return new Result(true, StatusCode.OK, "查询成功", brandService.findPage(page, size));
    }

    @ApiOperation("分页,按条件查询品牌列表")
    @PostMapping("/searchByPage/{page}/{size}")
    public Result searchByPage(@PathVariable Integer page, @PathVariable Integer size, @RequestBody Map<String, Object> searchMap) {
        return new Result(true, StatusCode.OK, "查询成功", brandService.findPage(page, size, searchMap));
    }

    @ApiOperation("根据分类名称查询品牌列表")
//    @GetMapping("/findListByCategoryName/{categoryName}") // 手机 :K
    @GetMapping("/findListByCategoryName")
    public Result findListByCategoryName(String categoryName) {
        System.out.println(categoryName);
        return new Result(true, StatusCode.OK, "查询成功", brandService.findListByCategoryName(categoryName));
    }

}
